from fastapi import FastAPI
from fastapi.responses import StreamingResponse
import asyncio
'''
测试 curl http://localhost:8080/start_review -H "Accept: text/event-stream"
 uvicorn sse_example:app --reload --host 0.0.0.0 --port 8080
'''
app = FastAPI()

async def run_review(task_id):
    # 模拟大模型审查耗时
    await asyncio.sleep(2)
    return {"task_id": task_id, "status": "completed", "result": "Passed"}

@app.get("/start_review")
async def start_review():
    async def event_generator():
        for i in range(1, 6):
            result = await run_review(i)
            yield f"data: {result}\n\n"
            await asyncio.sleep(0.1)  # 控制发送频率

    return StreamingResponse(event_generator(), media_type="text/event-stream")